<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import type { formData } from '@/views/login/type'
import { useChangePwdAPI, useVerifyAccountAndEmailAPI } from '@/http/userLogin'
import { ElMessage } from 'element-plus'
// import { changePasswordAPI } from '@/http/admin';

// 传入验证规则
const rules = inject('rules')
let goBackDialog = ref<boolean>(false)
let goBackPwdRef = ref()
let changePwdRef = ref()
let changeDialog = ref<boolean>(false)
// Dialog重置密码表单
const forgetPwd: formData = reactive({
  account: '',
  email: '',
  newPassword: '',
  twoPassword: '',
})
// Dialog查询邮箱和账号下一步的按钮
const nextStep = async () => {
  await goBackPwdRef.value.validate()
  const res = await useVerifyAccountAndEmailAPI(forgetPwd)
  if (res.account.status == 0 && res.email.status == 0) {
    ElMessage.success('查询成功！')
  } else if (res.account.status !== 0) {
    ElMessage.error(`${res.account.message}`)
  } else if (res.email.status !== 0) {
    ElMessage.error(`${res.email.message}`)
  }
  if (res.account.status === 0 && res.email.status === 0) {
    goBackDialog.value = false
    changeDialog.value = true
  }
}
// 提交修改后的密码
const onSubChangePwd = async () => {
  if (forgetPwd.newPassword == '' && forgetPwd.twoPassword == '') {
    ElMessage.error('密码为空，请填写密码!')
  } else {
    changeDialog.value = false
    await changePwdRef.value.validate()
    const res = await useChangePwdAPI(forgetPwd)
    ElMessage({
      type:
        forgetPwd.newPassword == forgetPwd.twoPassword ? 'success' : 'error',
      message:
        forgetPwd.newPassword == forgetPwd.twoPassword
          ? `${res.message}`
          : '失败',
    })
  }
}
defineExpose({
  goBackDialog,
  forgetPwd,
})
</script>

<template>
  <el-dialog v-model="goBackDialog" title="找回密码" width="400">
    <el-form
      :model="forgetPwd"
      label-width="auto"
      style="max-width: 600px"
      :rules="rules"
      ref="goBackPwdRef"
    >
      <el-form-item label="账号" prop="account">
        <el-input v-model="forgetPwd.account" placeholder="输入您的账号" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="forgetPwd.email" placeholder="输入您的邮箱" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="goBackDialog = false">取消</el-button>
        <el-button type="primary" @click="nextStep">下一步</el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog v-model="changeDialog" title="修改密码" width="400">
    <el-form
      :model="forgetPwd"
      label-width="auto"
      style="max-width: 600px"
      :rules="rules"
      ref="changePwdRef"
    >
      <el-form-item label="密码" prop="newPassword">
        <el-input
          v-model="forgetPwd.newPassword"
          type="password"
          placeholder="输入您的密码"
        />
      </el-form-item>
      <el-form-item label="再次确认密码" prop="twoPassword">
        <el-input
          v-model="forgetPwd.twoPassword"
          type="password"
          placeholder="再次输入您的密码"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="changeDialog = false">取消</el-button>
        <el-button type="primary" @click="onSubChangePwd">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss"></style>
